package com.qingfeng.client.service;

import com.qingfeng.client.bean.OrderStore;
import com.qingfeng.client.dao.OrderGoodsMapper;
import com.qingfeng.client.dao.OrderStoreMapper;
import com.qingfeng.client.util.TradeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/3/27 9:16 上午
 */
@Service
public class OrderService {

    @Resource
    private OrderStoreMapper orderStoreMapper;
    @Resource
    private OrderGoodsMapper orderGoodsMapper;


    /**
     * 获取指定交易状态的订单
     * @param stat
     * @return
     */
    public List<OrderStore> getByStat(int stat){
        return orderStoreMapper.getByStat(stat);
    }


    /**
     * 自动处理待收货订单
     *
     *  @param orderStoreId 订单id
     */
    public void dealRecvOrder(long orderStoreId){
        OrderStore orderStore = orderStoreMapper.selectById(orderStoreId);
        if (orderStore.getStat().equals(TradeEnum.TRADE_WAIT_RECV.getCode())) {
            orderStore.setStat(TradeEnum.TRADE_FINISH.getCode());
            orderStore.setFinishTime((int) (System.currentTimeMillis() / 1000));
            orderStoreMapper.updateById(orderStore);
            orderGoodsMapper.updateStatByOrderStoreId(TradeEnum.TRADE_FINISH.getCode(),orderStoreId);
        }
    }

    /**
     * 自动处理待评价订单
     * @param orderStoreId 订单id
     */
    public void dealCloseOrder(long orderStoreId){
        OrderStore orderStore = orderStoreMapper.selectById(orderStoreId);
        if(orderStore.getStat().equals(TradeEnum.TRADE_FINISH.getCode())) {
            orderStore.setStat(TradeEnum.TRADE_CLOSED.getCode());
            orderStore.setCloseTime((int) (System.currentTimeMillis() / 1000));
            orderStoreMapper.updateById(orderStore);
            orderGoodsMapper.updateStatByOrderStoreId(TradeEnum.TRADE_CLOSED.getCode(),orderStoreId);
        }
    }

}
